<template>
  <!-- <div></div> -->

  <div class="app-container">
    <div class="app-container">
      <el-row :gutter="12">
        <el-col :span="8">
          <el-card style="background-color: yellowgreen;" shadow="always">
            收入:<span>{{ this.statistics.s }}</span>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card style="background-color: #ff9933;" shadow="always">
            支出:<span>{{ this.statistics.z }}</span>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card style="background-color: #66ff00;" shadow="always">
            结余:<span>{{ this.statistics.balance }}</span>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-select
          v-model="queryParams.name"
          placeholder="请选择家庭成员"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in nameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收支类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择收支类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="收支时间" prop="hiaeDate">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.hiaeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择收支时间"
        >
        </el-date-picker>
      </el-form-item> -->

      <el-form-item label="收支时间" prop="hiaeDate"> </el-form-item>
      <el-date-picker
        v-model="dateRange"
        size="small"
        style="width: 240px"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
      >
      </el-date-picker>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:hiae:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hiae:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hiae:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:hiae:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="hiaeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="show" label="主键id" align="center" prop="id" />
      <el-table-column
        v-if="show"
        label="用户id"
        align="center"
        prop="userId"
      />

      <!-- <el-table-column
        label="修改时间"
        align="center"
        prop="updateDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="姓名" width="60" align="center" prop="name" />
      <el-table-column label="收支类型" width="75" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 's'" style="color:#37B328">收入</span>
          <span v-else style="color: red">支出</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" width="80" align="center" prop="remarks">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 's'" style="color:#37B328">{{
            scope.row.remarks
          }}</span>
          <span v-else style="color: red"> {{ scope.row.remarks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收/支 明细" align="center" prop="describes" />
      <el-table-column
        label="收/支时间"
        align="center"
        prop="hiaeDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hiaeDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:hiae:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:hiae:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家庭收支对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <!-- <el-input v-model="form.name" placeholder="请输入姓名" /> -->
          <el-select v-model="form.name" placeholder="请选择家庭成员">
            <el-option
              v-for="dict in nameOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="收支类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择收支类型">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="收/支时间" prop="hiaeDate">
          <el-date-picker
            clearable
            size="small"
            style="width: 190px"
            v-model="form.hiaeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择收/支时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收/支明细" prop="describes">
          <el-input
            type="textarea"
            v-model="form.describes"
            placeholder="请输入收/支明细"
          />
        </el-form-item>
        <el-form-item label="金额" prop="remarks">
          <el-input
            oninput="if(isNaN(value)) { value = null } if(value.indexOf('.')>0){value=value.slice(0,value.indexOf('.')+3)}"
            v-model="form.remarks"
            placeholder="请输入金额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHiae,
  getHiae,
  delHiae,
  addHiae,
  updateHiae,
  exportHiae
} from "@/api/system/hiae";
import { getToken } from "@/utils/auth";
export default {
  name: "Hiae",
  data() {
    return {
      show: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家庭收支表格数据
      hiaeList: [],
      //汇总数据
      statistics: [],
      // 收支类型字典
      statusOptions: [],
      // 家庭成员字典
      nameOptions: [],
      // 日期范围
      dateRange: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        type: null,
        createDate: null,
        updateDate: null,
        hiaeDate: [],
        hiaeDate1: null,
        hiaeDate2: null,
        describes: null,
        remarks: null
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请选择家庭成员", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        describes: [
          { required: true, message: "请输入收支明细", trigger: "blur" }
        ],
        remarks: [{ required: true, message: "请输入金额", trigger: "blur" }],
        type: [{ required: true, message: "请选择收支类型", trigger: "blur" }],
        hiaeDate: [
          { required: true, message: "请选择收支时间", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_hiae_type").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_hiae_name").then(response => {
      this.nameOptions = response.data;
    });
  },
  methods: {
    /** 查询家庭收支列表 */
    // getList() {
    //   this.loading = true;
    //   listHiae(this.queryParams).then(response => {
    //     this.hiaeList = response.rows;
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    getList() {
      this.loading = true;
      listHiae(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.hiaeList = response.rows;
          this.statistics = response.rows2;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        type: null,
        createDate: null,
        updateDate: null,
        hiaeDate: null,
        describes: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家庭收支";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getHiae(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改家庭收支";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHiae(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addHiae(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
              if (response.code === 999) {
                this.msg("请勿新增重复数据");
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除家庭收支编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delHiae(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有家庭收支数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportHiae(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
